package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.sale.PostImgApi;
import com.centaline.androidsalesblog.model.businissobject.PostImgBo;
import com.centaline.androidsalesblog.model.responseobject.PostImgRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;

/**
 * 租售房源图片
 * Created by guilin on 16/1/29.
 */
final class RequestPostImgOnSubscribe implements Observable.OnSubscribe<ArrayList<PostImgBo>> {

    private final Class<?> tag;
    private final String postId;

    public RequestPostImgOnSubscribe(Class<?> tag, String postId) {
        this.tag = tag;
        this.postId = postId;
    }

    @Override
    public void call(final Subscriber<? super ArrayList<PostImgBo>> subscriber) {
        new PostImgApi(tag, PostImgRo.class, new IResponseListener<PostImgRo>() {
            @Override
            public void onResponse(PostImgRo postImgRo) {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(postImgRo.getList());
                }
                subscriber.onCompleted();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                subscriber.onError(error);
            }
        }, postId).startRequest();
    }
}
